library(tidyverse)
library(lme4)
library(lmerTest)
library(logging)
library(mvtnorm)
library(mgcv)
# Compute the log-likelihood of a new dataset using a fit lme4 model.
logLik_test <- function(lm, test_X, test_y) {
predictions <- predict(lm, test_X, re.form=NA)
# Get std.dev. of residual, estimated from train data
stdev <- sigma(lm)
# For each prediction--observation, get the density p(obs | N(predicted, model_sigma)) and reduce
density <- sum(dnorm(test_y, predictions, stdev, log=TRUE))
return(density)
}
# Get per-prediction log-likelihood
logLik_test_per <- function(lm, test_X, test_y) {
predictions <- predict(lm, test_X, re.form=NA)
# Get std.dev. of residual, estimated from train data
stdev <- sigma(lm)
# For each prediction--observation, get the density p(obs | N(predicted, model_sigma))
densities <- dnorm(test_y, predictions, stdev, log=TRUE)
return(densities)
}
# Compute MSE of a new dataset using a fit lme4 model.
mse_test <- function(lm, test_X, test_y) {
return(mean((predict(lm, test_X, re.form=NA) - test_y) ^ 2))
}
#Sanity checks
#mylm <- gam(psychometric ~ s(surprisal, bs = "cr", k = 20) + s(prev_surp, bs = "cr", k = 20) + te(freq, len, bs = "cr") + te(prev_freq, prev_len, bs = "cr"), data=train_data)
#c(logLik(mylm), logLik_test(mylm, train_data, train_data$psychometric))
#logLik_test(mylm, test_data, test_data$psychometric)
data = read.csv("../data/harmonized_results.csv")
all_data = data %>%
mutate(seed = as.factor(seed)) %>%
group_by(corpus, model, training, seed) %>%
mutate(prev_surp = lag(surprisal),
prev_code = lag(code),
prev_len = lag(len),
prev_freq = lag(freq),
prev_surp = lag(surprisal),
prev2_freq = lag(prev_freq),
prev2_code = lag(prev_code),
prev2_len = lag(prev_len),
prev2_surp = lag(prev_surp),
prev3_freq = lag(prev2_freq),
prev3_code = lag(prev2_code),
prev3_len = lag(prev2_len),
prev3_surp = lag(prev2_surp)) %>%
ungroup() %>%
# Filter back three for the dundee corpus. Filter back 1 for all other corpora
# NB this effectively removes all zero-surprisal rows, since early-sentence tokens don't have contiguous token history
filter((corpus == "dundee" & code == prev2_code + 2) | (corpus != "dundee" & code == prev_code + 1)) %>%
select(-prev_code, -prev2_code, -prev3_code) %>%
drop_na()
all_data = all_data %>%
mutate(
model = as.character(model),
model = if_else(model == "gpt-2", "gpt2", model),
model = as.factor(model)) %>%
filter(!(model == "gpt2" & corpus == "bnc-brown"))
missing_rows = all_data %>% complete(nesting(corpus, code), nesting(model, training, seed)) %>%
group_by(corpus, code) %>%
filter(sum(is.na(surprisal)) > 0) %>%
ungroup() %>%
anti_join(all_data, by=c("corpus", "code", "model", "training", "seed"))
missing_rows %>% ggplot(aes(x=corpus, fill=factor(paste(model,training)))) + geom_bar(position=position_dodge(width=0.8))
print(missing_rows %>% group_by(model, training, seed, corpus) %>% summarise(n=n())) %>% arrange(desc(n))
# Compute the ideal number of model--seed--training observations per token.
to_drop = all_data %>%
group_by(corpus, code) %>% summarise(n = n()) %>% ungroup() %>%
group_by(corpus) %>% mutate( max_n = max(n)) %>% ungroup() %>%
filter(max_n != n) %>%
select(code, corpus)
#to_drop = all_data %>% group_by(corpus, code) %>% filter(n() != ideal_token_obs_count) %>% ungroup()
loginfo(paste("Dropping", nrow(to_drop), "observations corresponding to corpus tokens which are missing observations for some model."))
[0m2020-05-08 12:02:51 INFO::Dropping 12693 observations corresponding to corpus tokens which are missing observations for some model.[0m[0m[0m
loginfo(paste("Dropping", to_drop %>% group_by(corpus, code) %>% n_groups(), "tokens which are missing observations for some model."))
[0m2020-05-08 12:02:51 INFO::Dropping 12693 tokens which are missing observations for some model.[0m[0m[0m
all_data = all_data %>% anti_join(to_drop %>% group_by(corpus, code), by=c("corpus", "code"))
loginfo(paste("After drop,", nrow(all_data), "observations (", all_data %>% group_by(corpus, code) %>% n_groups(), " tokens) remain."))
[0m2020-05-08 12:02:51 INFO::After drop, 972215 observations ( 35475 tokens) remain.[0m[0m[0m
to_drop_zero_surps = all_data %>% group_by(corpus, code) %>% filter(any(surprisal == 0)) %>% ungroup()
loginfo(paste("Dropping", nrow(to_drop_zero_surps), "observations corresponding to corpus tokens which have surprisal zeros for some model."))
[0m2020-05-08 12:02:59 INFO::Dropping 135 observations corresponding to corpus tokens which have surprisal zeros for some model.[0m[0m[0m
loginfo(paste("Dropping", to_drop_zero_surps %>% group_by(corpus, code) %>% n_groups(), "tokens which have surprisal zeros for some model."))
[0m2020-05-08 12:02:59 INFO::Dropping 5 tokens which have surprisal zeros for some model.[0m[0m[0m
all_data = all_data %>% anti_join(to_drop_zero_surps %>% group_by(corpus, code), by=c("corpus", "code"))
loginfo(paste("After drop,", nrow(all_data), "observations (", all_data %>% group_by(corpus, code) %>% n_groups(), " tokens) remain."))
[0m2020-05-08 12:03:00 INFO::After drop, 972080 observations ( 35470 tokens) remain.[0m[0m[0m
to_drop_zero_psychs = all_data %>% group_by(corpus, code) %>% filter(any(psychometric == 0)) %>% ungroup()
loginfo(paste("Dropping", nrow(to_drop_zero_psychs), "observations corresponding to corpus tokens which have surprisal zeros for some model."))
[0m2020-05-08 12:03:00 INFO::Dropping 14420 observations corresponding to corpus tokens which have surprisal zeros for some model.[0m[0m[0m
loginfo(paste("Dropping", to_drop_zero_psychs %>% group_by(corpus, code) %>% n_groups(), "tokens which have surprisal zeros for some model."))
[0m2020-05-08 12:03:00 INFO::Dropping 515 tokens which have surprisal zeros for some model.[0m[0m[0m
all_data = all_data %>% anti_join(to_drop_zero_psychs %>% group_by(corpus, code), by=c("corpus", "code"))
loginfo(paste("After drop,", nrow(all_data), "observations (", all_data %>% group_by(corpus, code) %>% n_groups(), " tokens) remain."))
[0m2020-05-08 12:03:01 INFO::After drop, 957660 observations ( 34955 tokens) remain.[0m[0m[0m
# Compute linear model stats for the given training data subset and full test data.
# Automatically subsets the test data to match the relevant group for which we are training a linear model.
get_lm_data <- function(df, test_data, formula, fold, store_env) {
#this_lm <- gam(formula, data=df);
this_lm = lm(formula, data=df)
this_test_data <- semi_join(test_data, df, by=c("training", "model", "seed", "corpus"));
# Save lm to the global env so that we can access residuals later.
lm_name = paste(unique(paste(df$model, df$training, df$seed, df$corpus))[1], fold)
assign(lm_name, this_lm, envir=store_env)
summarise(df,
log_lik = as.numeric(logLik(this_lm, REML = F)),
test_lik = logLik_test(this_lm, this_test_data, this_test_data$psychometric),
test_mse = mse_test(this_lm, this_test_data, this_test_data$psychometric))
}
# For a previously fitted lm stored in store_env, get the residuals on test data of the relevant data subset.
get_lm_residuals <- function(df, fold, store_env) {
# Retrieve the relevant lm.
lm_name = paste(unique(paste(df$model, df$training, df$seed, df$corpus))[1], fold)
this_lm <- get(lm_name, envir=store_env)
mutate(df,
likelihood = logLik_test_per(this_lm, df, df$psychometric),
resid = df$psychometric - predict(this_lm, df, re.form=NA))
}
# Compute per-example delta-log-likelihood for the given test fold.
get_lm_delta_log_lik <- function(test_data, fold, baseline_env, full_env) {
lm_name = paste(unique(paste(test_data$model, test_data$training, test_data$seed, test_data$corpus))[1], fold)
baseline_lm <- get(lm_name, envir=baseline_env)
full_lm <- get(lm_name, envir=full_env)
delta_log_lik = logLik_test_per(full_lm, test_data, test_data$psychometric) - logLik_test_per(baseline_lm, test_data, test_data$psychometric)
return(cbind(test_data, delta_log_lik=delta_log_lik))
}
#####
# Define regression formulae.
# Eye-tracking regression: only use surprisal and previous surprisal; SPRT regression: use 2-back features.
#baseline_rt_regression = psychometric ~ te(freq, len, bs = "cr") + te(prev_freq, prev_len, bs = "cr")
#baselie_sprt_regression = psychometric ~ te(freq, len, bs = "cr") + te(prev_freq, prev_len, bs = "cr") + te(prev2_freq, prev2_len, bs = "cr")
#full_rt_regression = (psychometric ~ s(surprisal, bs = "cr", k = 20) + s(prev_surp, bs = "cr", k = 20)
#+ te(freq, len, bs = "cr") + te(prev_freq, prev_len, bs = "cr"))
#full_sprt_regression = (psychometric ~ s(surprisal, bs = "cr", k = 20) + s(prev_surp, bs = "cr", k = 20) + s(prev2_surp, bs = "cr", k = 20)
#+ te(freq, len, bs = "cr") + te(prev_freq, prev_len, bs = "cr") + te(prev2_freq, prev2_len, bs = "cr"))
baseline_rt_regression = psychometric ~ freq + prev_freq + prev2_freq + prev3_freq + len + prev_len + prev2_len + prev3_len
baseline_sprt_regression = psychometric ~ freq + prev_freq + len + prev_len
full_rt_regression = psychometric ~ surprisal + prev_surp + prev2_surp + prev3_surp + freq + prev_freq + prev2_freq + prev3_freq + len + prev_len + prev2_len + prev3_len
full_sprt_regression = psychometric ~ surprisal + prev_surp + freq + prev_freq + len + prev_len
#####
# Prepare frames/environments for storing results/objects.
baseline_results = data.frame()
full_model_results = data.frame()
baseline_residuals = data.frame()
full_residuals = data.frame()
log_lik_deltas = data.frame()
#Randomly shuffle the data
all_data<-all_data[sample(nrow(all_data)),]
#Create K equally size folds
K = 5
folds <- cut(seq(1,nrow(all_data)),breaks=K,labels=FALSE)
#Perform 10 fold cross validation
# Fit models for some fold of the data.
baseline_corpus = function(corpus, df, test_data, fold, env) {
if(corpus == "dundee") {
get_lm_data(df, test_data, baseline_rt_regression, fold, env)
} else {
get_lm_data(df, test_data, baseline_sprt_regression, fold, env)
}
}
full_model_corpus = function(corpus, df, test_data, fold, env) {
if(corpus[1] == "dundee") {
get_lm_data(df, test_data, full_rt_regression, fold, env)
} else {
get_lm_data(df, test_data, full_sprt_regression, fold, env)
}
}
# Prepare a new Environment in which we store fitted LMs, which we'll query later for residuals and other metrics.
baseline_env = new.env()
full_env = new.env()
for(i in 1:K) {
#Segement your data by fold using the which() function
testIndexes <- which(folds==i, arr.ind=TRUE)
test_data <- all_data[testIndexes, ]
train_data <- all_data[-testIndexes, ]
# Compute a baseline linear model for each model--training--seed--RT-corpus combination.
baselines = train_data %>%
group_by(model, training, seed, corpus) %>%
print(model) %>%
do(baseline_corpus(unique(.$corpus), ., test_data, i, baseline_env)) %>%
ungroup() %>%
mutate(seed = as.factor(seed),
fold = i)
baseline_results = rbind(baseline_results, baselines)
# Compute a full linear model for each model--training--seed-RT-corpus combination
full_models = train_data %>%
group_by(model, training, seed, corpus) %>%
do(full_model_corpus(unique(.$corpus), ., test_data, i, full_env)) %>%
ungroup() %>%
mutate(seed = as.factor(seed),
fold = i)
full_model_results = rbind(full_model_results, full_models)
# Compute delta-log-likelihoods
fold_log_lik_deltas = test_data %>%
group_by(model, training, seed, corpus) %>%
do(get_lm_delta_log_lik(., i, baseline_env, full_env)) %>%
ungroup()
log_lik_deltas = rbind(log_lik_deltas, fold_log_lik_deltas)
fold_baseline_residuals = test_data %>%
group_by(model, training, seed, corpus) %>%
do(get_lm_residuals(., i, baseline_env)) %>%
ungroup()
baseline_residuals = rbind(baseline_residuals, fold_baseline_residuals)
fold_full_residuals = test_data %>%
group_by(model, training, seed, corpus) %>%
do(get_lm_residuals(., i, full_env)) %>%
ungroup()
full_residuals = rbind(full_residuals, fold_full_residuals)
}
|================================================================================================================================================== | 67% ~1 s remaining
|=========================================================================================================================================================== | 71% ~1 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|==================================================================================================================================================================================== | 82% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~1 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|============================================================================================================================================================================================================= | 94% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|======================================================================================================================================================== | 70% ~1 s remaining
|=========================================================================================================================================================== | 71% ~1 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|===================================================================================================================================================== | 68% ~1 s remaining
|=========================================================================================================================================================== | 71% ~1 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
|============================================================================================================================================================== | 72% ~1 s remaining
|=================================================================================================================================================================== | 75% ~1 s remaining
|====================================================================================================================================================================== | 76% ~1 s remaining
|============================================================================================================================================================================== | 80% ~1 s remaining
|================================================================================================================================================================================= | 81% ~1 s remaining
|====================================================================================================================================================================================== | 84% ~0 s remaining
|=============================================================================================================================================================================================== | 87% ~0 s remaining
|======================================================================================================================================================================================================= | 91% ~0 s remaining
|=============================================================================================================================================================================================================== | 95% ~0 s remaining
|======================================================================================================================================================================================================================== | 99% ~0 s remaining
#write.csv(full_residuals, "../data/analysis_checkpoints/full_residuals.csv")
#write.csv(baseline_residuals, "../data/analysis_checkpoints/baseline_residuals.csv")
model_deltas = log_lik_deltas %>%
group_by(model, training, seed, corpus) %>%
summarise(mean_delta_log_lik = mean(delta_log_lik),
sem_delta_log_lik = sd(delta_log_lik) / sqrt(length(delta_log_lik)))
write.csv(full_model_results, "../data/analysis_checkpoints/full_model_result.csv")
write.csv(baseline_results, "../data/analysis_checkpoints/baseline_results.csv")
#full_model_results = read.csv("../data/analysis_checkpoints/ffull_model_results.csv")
#baseline_results = read.csv("../data/analysis_checkpoints/fbaseline_resultsb.csv")
metric <- "ΔLogLik"
#metric <- "-ΔMSE"
# # Select the relevant metric.
model_deltas = model_deltas %>%
# Retrieve the current test metric
mutate(delta_test_mean = mean_delta_log_lik,
delta_test_sem = sem_delta_log_lik) %>%
# mutate(delta_test_mean = mean_delta_mse,
# delta_test_sem = sem_delta_mse)
# Remove the raw metrics.
select(-mean_delta_log_lik, -sem_delta_log_lik,
#-mean_delta_mse, -sem_delta_mse
)
model_deltas
# Sanity check: training on train+test data should yield improved performance over training on just training data. (When evaluating on test data.)
# full_baselines = all_data %>%
# group_by(model, training, seed, corpus) %>%
# summarise(baseline_train_all_test_lik = logLik_test(lm(psychometric ~ len + freq + sent_pos, data=.), semi_join(test_data, ., by=c("training", "model", "seed", "corpus")), semi_join(test_data, ., by=c("training", "model", "seed", "corpus"))$psychometric)) %>%
# ungroup()
# full_baselines
#
# full_baselines %>%
# right_join(baselines, by=c("seed", "training", "model", "corpus")) %>%
# mutate(delta=baseline_train_all_test_lik-baseline_test_lik) %>%
# select(-baseline_lik) # %>%
# #select(-baseline_test_lik, -baseline_train_all_test_lik, -baseline_lik, -baseline_test_mse)
language_model_data = read.csv("../data/model_metadata.csv") %>%
mutate(model = as.character(model),
model = if_else(model == "gpt-2", "gpt2", model),
model = as.factor(model)) %>%
mutate(train_size = case_when(str_starts(training, "bllip-lg") ~ 42,
str_starts(training, "bllip-md") ~ 15,
str_starts(training, "bllip-sm") ~ 5,
str_starts(training, "bllip-xs") ~ 1),
# Training vocabulary usually covaries with the training corpus.
# But BPE models share a vocabulary across training corpora.
training_vocab=as.factor(ifelse(str_detect(training, "gptbpe"), "gptbpe", as.character(training)))
) %>%
mutate(seed = as.factor(seed)) %>%
select(-pid, -test_loss) %>%
distinct(model, training, seed, .keep_all = TRUE)
table(language_model_data$seed)
0 111 120 922 1111 3602 4301 7245 7877 28066 28068 44862 51272 64924 1581807512 1581807578 1581861474 1581955288 1582126320
4 7 6 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1
table(model_deltas$seed)
111 120 607 922 1111 3602 4301 7245 7877 28066 28068 44862 51272 64924 1581807512 1581807578 1581861474 1581955288 1582126320
9 9 1 9 14 3 3 3 3 3 3 3 3 3 2 2 2 2 2
First join delta-metric data with model auxiliary data.
model_deltas = model_deltas %>%
merge(language_model_data, by = c("seed", "training", "model"), all=T) %>%
drop_na()
model_deltas
Also join on the original linear model data, rather than collapsing to delta-metrics. This will support regressions later on that don’t collapse across folds.
# Exclude ordered-neurons from all analyses.
model_deltas <- model_deltas %>%
filter(model != "ordered-neurons")
all_data %>% ggplot(aes(x=corpus)) + geom_bar()
print(all_data %>% group_by(corpus) %>% summarise(n=n()))
all_data %>%
ggplot(aes(x=freq, color=corpus)) + geom_density()
all_data %>%
ggplot(aes(x=len, color=corpus)) + geom_density()
all_data %>%
ggplot(aes(x=surprisal, color=corpus)) + geom_density()
model_deltas %>%
ggplot(aes(x=sg_score, y=delta_test_mean)) +
geom_errorbar(aes(ymin=delta_test_mean-delta_test_sem, ymax=delta_test_mean+delta_test_sem)) +
geom_smooth(method="lm", se=T) +
geom_point(stat="identity", position="dodge", alpha=1, size=3, aes(color=training_vocab, shape=model)) +
ylab(metric) +
xlab("Syntax Generalization Score") +
ggtitle("Syntactic Generalization vs. Predictive Power") +
scale_color_manual(values = c("bllip-lg"="#440154FF",
"bllip-md"="#39568CFF",
"bllip-sm"="#1F968BFF",
"bllip-xs"="#73D055FF",
"gptbpe"="#888888")) +
facet_grid(~corpus, scales="free") +
theme(axis.text=element_text(size=14),
strip.text.x = element_text(size=14),
legend.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = "bottom")
#ggsave("./cogsci_images/sg_loglik.png",height=5,width=6)
We control for effects of perplexity by relating the residuals of a performance ~ PPL regression to SG score.
# Prepare a residualized regression for x1 onto y, controlling for the effects of x2.
d_resid = model_deltas %>%
drop_na() %>%
# Residualize delta metric w.r.t PPL for each model--training--seed
group_by(corpus) %>%
mutate(resid.delta = resid(lm(delta_test_mean ~ training:test_ppl))) %>%
ungroup() %>%
# Residualize SG score w.r.t. PPL for each training vocabulary
group_by(training_vocab) %>%
# NB no need for training:ppl interaction, since we're within-group.
mutate(resid.sg = resid(lm(sg_score ~ test_ppl))) %>%
ungroup() %>%
# Compute summary statistics across model--training--seed--corpus.
group_by(model, training_vocab, corpus, seed) %>%
summarise(resid.delta.mean = mean(resid.delta),
resid.delta.sem = sd(resid.delta) / sqrt(length(resid.delta)),
resid.sg.mean = mean(resid.sg),
resid.sg.sem = sd(resid.sg) / sqrt(length(resid.sg)))
# Now plot residual vs SG
d_resid %>%
#filter(corpus != "bnc-brown") %>%
ggplot(aes(x=resid.sg.mean, y=resid.delta.mean)) +
geom_errorbar(aes(xmin=resid.sg.mean - resid.sg.sem,
xmax=resid.sg.mean + resid.sg.sem,
ymin=resid.delta.mean - resid.delta.sem,
ymax=resid.delta.mean + resid.delta.sem), alpha=0.3) +
geom_smooth(method="lm", se=T) +
geom_point(stat="identity", position="dodge", alpha=1, size=4, aes(shape=model, color=training_vocab)) +
ylab(paste("Residual", metric)) +
xlab("Residual Syntax Generalization Score") +
ggtitle("Syntactic Generalization vs. Predictive Power") +
scale_color_manual(values = c("bllip-lg"="#440154FF",
"bllip-md"="#39568CFF",
"bllip-sm"="#1F968BFF",
"bllip-xs"="#73D055FF",
"gptbpe"="#888888")) +
facet_grid(.~corpus, scales="free") +
theme(axis.text=element_text(size=14),
strip.text.x = element_text(size=14),
legend.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = "right")
Ignoring unknown aesthetics: xmin, xmax
ggsave("../images/cuny2020/ppl_sg.png",height=4.5,width=11)
do_stepwise_regression = function(cur_corpus) {
regression_data = model_deltas %>%
filter(corpus == cur_corpus)
print("----------------------")
print(cur_corpus)
lm1 = lm(delta_test_mean ~ training_vocab:test_ppl, data = regression_data)
lm2 = lm(delta_test_mean ~ training_vocab:test_ppl + sg_score, data = regression_data)
print(anova(lm1, lm2))
summary(lm2)
}
#do_stepwise_regression("bnc-brown")
do_stepwise_regression("dundee")
[1] "----------------------"
[1] "dundee"
Analysis of Variance Table
Model 1: delta_test_mean ~ training_vocab:test_ppl
Model 2: delta_test_mean ~ training_vocab:test_ppl + sg_score
Res.Df RSS Df Sum of Sq F Pr(>F)
1 22 1.200e-04
2 21 1.446e-05 1 0.00010554 153.26 4.089e-11 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Call:
lm(formula = delta_test_mean ~ training_vocab:test_ppl + sg_score,
data = regression_data)
Residuals:
Min 1Q Median 3Q Max
-0.0014411 -0.0004662 0.0002021 0.0004733 0.0016726
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.817e-03 5.052e-04 19.434 6.66e-15 ***
sg_score -1.107e-02 8.939e-04 -12.380 4.09e-11 ***
training_vocabbllip-lg:test_ppl 1.350e-05 7.329e-06 1.842 0.079589 .
training_vocabbllip-md:test_ppl 1.333e-05 6.207e-06 2.147 0.043640 *
training_vocabbllip-sm:test_ppl -8.994e-06 4.104e-06 -2.192 0.039806 *
training_vocabbllip-xs:test_ppl -8.718e-06 1.858e-06 -4.693 0.000124 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.0008298 on 21 degrees of freedom
Multiple R-squared: 0.8961, Adjusted R-squared: 0.8714
F-statistic: 36.23 on 5 and 21 DF, p-value: 1.231e-09
do_stepwise_regression("natural-stories")
[1] "----------------------"
[1] "natural-stories"
Analysis of Variance Table
Model 1: delta_test_mean ~ training_vocab:test_ppl
Model 2: delta_test_mean ~ training_vocab:test_ppl + sg_score
Res.Df RSS Df Sum of Sq F Pr(>F)
1 22 0.00011832
2 21 0.00004743 1 7.0891e-05 31.387 1.467e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Call:
lm(formula = delta_test_mean ~ training_vocab:test_ppl + sg_score,
data = regression_data)
Residuals:
Min 1Q Median 3Q Max
-0.0041286 -0.0007988 0.0001290 0.0009035 0.0020338
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.606e-03 9.149e-04 10.500 8.19e-10 ***
sg_score -9.070e-03 1.619e-03 -5.602 1.47e-05 ***
training_vocabbllip-lg:test_ppl -2.748e-05 1.327e-05 -2.070 0.050956 .
training_vocabbllip-md:test_ppl -1.753e-05 1.124e-05 -1.560 0.133820
training_vocabbllip-sm:test_ppl -3.041e-05 7.432e-06 -4.092 0.000521 ***
training_vocabbllip-xs:test_ppl -1.127e-05 3.365e-06 -3.350 0.003033 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.001503 on 21 degrees of freedom
Multiple R-squared: 0.6499, Adjusted R-squared: 0.5665
F-statistic: 7.795 on 5 and 21 DF, p-value: 0.0002786
model_deltas %>%
ggplot(aes(x=test_ppl, y=delta_test_mean, color=training_vocab)) +
geom_errorbar(aes(ymin=delta_test_mean-delta_test_sem, ymax=delta_test_mean+delta_test_sem), alpha=0.4) +
#geom_smooth(method="lm", se=F) +
geom_point(stat="identity", position="dodge", alpha=1, size=4, aes(shape=model)) +
ylab(metric) +
xlab("Test Perplexity") +
#coord_cartesian(ylim = c(1, 16)) +
ggtitle("Test Perplexity vs. Predictive Power") +
scale_color_manual(values = c("bllip-lg"="#440154FF",
"bllip-md"="#39568CFF",
"bllip-sm"="#1F968BFF",
"bllip-xs"="#73D055FF",
"gptbpe"="#888888")) +
facet_grid(~corpus, scales="free") +
#coord_cartesian(ylim = c(0, 150)) +
theme(axis.text=element_text(size=12),
strip.text.x = element_text(size=12),
legend.text=element_text(size=12),
axis.title=element_text(size=12),
legend.position = "right")
ggsave("../images/cuny2020/ppl_loglik.png",height=4.5,width=11)
model_deltas %>%
mutate(train_size = log(train_size)) %>%
ggplot(aes(x=train_size, y=delta_test_mean, color=model)) +
geom_errorbar(aes(ymin=delta_test_mean-delta_test_sem, ymax=delta_test_mean+delta_test_sem), width = 0.1) +
geom_smooth(method="lm", se=T, alpha=0.5) +
geom_point(stat="identity", position="dodge", alpha=1, size=3) +
ylab(metric) +
xlab("Log Million Training Tokens") +
ggtitle("Training Size vs. Predictive Power") +
facet_grid(corpus~model, scales="free") +
#scale_color_manual(values = c("#A42EF1", "#3894C8")) +
theme(axis.text=element_text(size=14),
strip.text.x = element_text(size=14),
legend.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = "bottom")
#ggsave("./cogsci_images/training_loglik.png",height=5,width=6)
model_deltas %>%
mutate(train_size = log(train_size)) %>%
ggplot(aes(x=train_size, y=sg_score, color=model)) +
geom_smooth(method="lm", se=T, alpha=0.5) +
geom_point(stat="identity", position="dodge", alpha=1, size=3) +
ylab("SG SCore") +
xlab("Log Million Training Tokens") +
ggtitle("Training Size vs. Syntactic Generalization") +
#scale_color_manual(values = c("#A42EF1", "#3894C8")) +
facet_grid(~model, scales="free") +
theme(axis.text=element_text(size=14),
strip.text.x = element_text(size=14),
legend.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = "bottom")
#ggsave("./cogsci_images/training_sg.png",height=5,width=6)
all_data %>%
#filter(surprisal < 15, surprisal > 0) %>%
ggplot(aes(x=surprisal, y=psychometric, color=training)) +
stat_smooth(se=T, alpha=0.5) +
#geom_errorbar(color="black", width=.2, position=position_dodge(width=.9), alpha=0.3) +
#geom_point(stat="identity", position="dodge", alpha=1, size=3) +
ylab("Processing Time (ms)") +
xlab("Surprisal (bits)") +
ggtitle("Surprisal vs. Reading Time / Gaze Duration") +
facet_grid(corpus~model, scales = "free") +
# scale_color_manual(values = c("bllip-lg"="#440154FF",
# "bllip-md"="#39568CFF",
# "bllip-sm"="#1F968BFF",
# "bllip-xs"="#73D055FF",
# "bllip-lg-gptbpe"="#888888",
# "bllip-md-gptbpe"="#888888",
# "bllip-sm-gptbpe"="#888888",
# "bllip-xs-gptbpe"="#888888")) +
theme(axis.text=element_text(size=14),
axis.text.y = element_text(size = 10),
strip.text.x = element_text(size=14),
legend.text=element_text(size=14),
axis.title=element_text(size=18),
legend.position = "right")
ggsave("../images/cuny2020/surp_corr.png",height=4.5,width=12)
all_data %>%
#filter(surprisal < 15, surprisal > 0) %>%
filter(model == "vanilla") %>%
ggplot(aes(x=surprisal, y=psychometric)) +
#stat_smooth(se=T, alpha=0.5) +
#geom_errorbar(color="black", width=.2, position=position_dodge(width=.9), alpha=0.3) +
geom_point(alpha=0.1) + #stat="identity", position="dodge", alpha=1, size=3) +
ylab("Processing Time (ms)") +
xlab("Surprisal (bits)") +
ggtitle("Surprisal vs. Reading Time / Gaze Duration: Vanilla") +
facet_grid(corpus~training, scales = "free")
# scale_color_manual(values = c("bllip-lg"="#440154FF",
# "bllip-md"="#39568CFF",
# "bllip-sm"="#1F968BFF",
# "bllip-xs"="#73D055FF",
# "bllip-lg-gptbpe"="#888888",
# "bllip-md-gptbpe"="#888888",
# "bllip-sm-gptbpe"="#888888",
# "bllip-xs-gptbpe"="#888888"))
full_residuals %>% #filter(model == "gpt2", str_detect(training, "gptbpe")) %>%
mutate(training_vocab=as.factor(ifelse(str_detect(training, "gptbpe"), "gptbpe", as.character(training)))) %>%
ggplot(aes(x=surprisal, y=resid, color=training)) +
facet_grid(~corpus) +
geom_smooth()
full_residuals %>% right_join(baseline_residuals, by=c("corpus", "code", "model", "training", "seed"), suffix=c(".full", ".baseline")) %>%
mutate(resid_delta=resid.full - resid.baseline) %>%
ggplot(aes(x=surprisal.full, y=resid_delta, color=training)) +
facet_grid(~corpus) +
geom_smooth()